import Taro from '@tarojs/taro'

// 检查云开发是否初始化
const checkCloudInit = () => {
  if (!Taro.cloud) {
    throw new Error('云开发未初始化')
  }
}

// 获取数据库实例
const getDB = () => {
  checkCloudInit()
  return Taro.cloud.database()
}

// 债务相关操作
export const debtService = {
  // 添加债务
  async addDebt(debt) {
    try {
      const db = getDB()
      const result = await db.collection('debts').add({
        data: {
          ...debt,
          createTime: db.serverDate()
        }
      })
      return { success: true, data: result }
    } catch (error) {
      console.error('添加债务失败:', error)
      return { success: false, error: error.message }
    }
  },

  // 获取债务列表
  async getDebts() {
    try {
      const db = getDB()
      const result = await db.collection('debts').get()
      return { success: true, data: result.data }
    } catch (error) {
      console.error('获取债务列表失败:', error)
      return { success: false, error: error.message }
    }
  },

  // 更新债务
  async updateDebt(id, data) {
    try {
      const db = getDB()
      const result = await db
        .collection('debts')
        .doc(id)
        .update({
          data: {
            ...data,
            updateTime: db.serverDate()
          }
        })
      return { success: true, data: result }
    } catch (error) {
      console.error('更新债务失败:', error)
      return { success: false, error: error.message }
    }
  },

  // 删除债务
  async deleteDebt(id) {
    try {
      const db = getDB()
      const result = await db.collection('debts').doc(id).remove()
      return { success: true, data: result }
    } catch (error) {
      console.error('删除债务失败:', error)
      return { success: false, error: error.message }
    }
  }
}

// 债权人相关操作
export const debtorService = {
  // 获取单个债务人
  async getDebtor(id) {
    try {
      const db = getDB()
      const result = await db.collection('debtors').doc(id).get()
      return { success: true, data: result.data }
    } catch (error) {
      console.error('获取债务人失败:', error)
      return { success: false, error: error.message }
    }
  },

  // 添加债权人
  async addDebtor(debtor) {
    try {
      const db = getDB()
      const result = await db.collection('debtors').add({
        data: {
          ...debtor,
          createTime: db.serverDate()
        }
      })
      return { success: true, data: result }
    } catch (error) {
      console.error('添加债权人失败:', error)
      return { success: false, error: error.message }
    }
  },

  // 获取债权人列表
  async getDebtors() {
    try {
      const db = getDB()
      const result = await db.collection('debtors').get()
      return { success: true, data: result.data }
    } catch (error) {
      console.error('获取债权人列表失败:', error)
      return { success: false, error: error.message }
    }
  },

  // 更新债权人
  async updateDebtor(id, data) {
    try {
      const db = getDB()
      const result = await db
        .collection('debtors')
        .doc(id)
        .update({
          data: {
            ...data,
            updateTime: db.serverDate()
          }
        })
      return { success: true, data: result }
    } catch (error) {
      console.error('更新债权人失败:', error)
      return { success: false, error: error.message }
    }
  },

  // 删除债权人
  async deleteDebtor(id) {
    try {
      const db = getDB()
      const result = await db.collection('debtors').doc(id).remove()
      return { success: true, data: result }
    } catch (error) {
      console.error('删除债权人失败:', error)
      return { success: false, error: error.message }
    }
  }
}
